﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sbook.Shopping
{
    public partial class XuLyMuaHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string Id = Request.Form["d"];
                string Price = Request.Form["P"];
                string PImg = Request.Form["PImg"];
                string PName = Request.Form["PName"];

                Price = Price.Substring(0, Price.Length - 2);
                Shopping.ShoppingItem item = new Shopping.ShoppingItem();
                item.ProductId = Convert.ToInt32(Id);
                item.Quantity = 1;
                item.Price = Convert.ToDecimal(Price);
                item.PName = PName;
                item.PImg = PImg;
                if (Data.Mysession.Session == null)
                {
                    item.UId = "";
                }
                else
                {
                    item.UId = Data.Mysession.Session.UId;
                }
                Shopping.ShoppingCart.Instance.Add(item);
                Response.Write("Giỏ hàng của bạn có thêm 1 sản phẩm ' " + item.PName.ToUpper() + "'");
                //Them vao hoa don mua hang
                //if (Business.DeliveryService.Delivery_GetByAll().Find(d => d.UId == item.UId) == null)
                //{
                //    Data.DeliveryInfo obj = new Data.DeliveryInfo();
                //    obj.DeliveryAdd = "";
                //    obj.DeliveryClass = "";
                //    obj.DeliveryCode = "";
                //    obj.DeliveryDate = DateTime.Now.ToString();
                //    obj.DeliveryEmail = "";
                //    obj.DeliveryLocation = "";
                //    obj.DeliveryName = "";
                //    obj.DeliveryPhone = "";
                //    obj.DeliveryPrice = "";//lam tong tien trong gio hang
                //    obj.DeliveryShippingPoint = "";
                //    obj.DeliveryStatus = "";
                //    obj.DeliveryUniversity = "";
                //    obj.PayId = "1";
                //    obj.UId = item.UId;

                //    Business.DeliveryService.Delivery_Insert(obj);

                //    //xu ly insert vao chi tiet hoa don 
                //    Data.DeliveryDetailInfo objDetail = new Data.DeliveryDetailInfo();
                //    objDetail.DeliveryId = Business.DeliveryService.Delivery_GetByAll().Find(od => od.UId == item.UId).DeliveryId;
                //    objDetail.Price = Price;
                //    objDetail.Qty = "1";
                //    objDetail.Remark = "";
                //    objDetail.Uom = "";
                //    objDetail.ProductId = Id;
                //    Business.DeliveryDetailService.DeliveryDetail_Insert(objDetail);

                //}
                //else
                //{

                //    if (Business.DeliveryDetailService.DeliveryDetail_GetByAll().Find(od => od.ProductId == Id) == null)
                //    {
                //        Data.DeliveryDetailInfo objDetail1 = new Data.DeliveryDetailInfo();
                //        objDetail1.DeliveryId = Business.DeliveryService.Delivery_GetByAll().Find(od => od.UId == item.UId).DeliveryId;
                //        objDetail1.Price = Price;
                //        objDetail1.Qty = "1";
                //        objDetail1.Remark = "";
                //        objDetail1.Uom = "";
                //        objDetail1.ProductId = Id;
                //        Business.DeliveryDetailService.DeliveryDetail_Insert(objDetail1);
                //    }
                //    else
                //    {
                //        Data.DeliveryDetailInfo obj = Business.DeliveryDetailService.DeliveryDetail_GetByAll().Find(od => od.ProductId == Id);
                //        obj.Qty = Convert.ToString(Convert.ToInt32(obj.Qty) + 1);
                //        Business.DeliveryDetailService.DeliveryDetail_Update(obj);


                //    }
                //}

                //Response.Write(Price);
            }
        }
    }
}